package com.xie.smfs.mixin.client;

import com.xie.smfs.effect.ICurseEffect;
import com.xie.smfs.item.GhostScissorsItem;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Collection;
import java.util.stream.Collectors;

@Environment(EnvType.CLIENT) // 确保只在客户端执行
@Mixin(LivingEntity.class)
public abstract class LivingEntityMixin {

    @Unique
    private boolean smfs$hasGhostScissors() {
        // 检查当前实体是否是客户端玩家
        LivingEntity self = (LivingEntity) (Object) this;
        if (self instanceof AbstractClientPlayerEntity) {
            ItemStack mainHand = self.getMainHandStack();
            ItemStack offHand = self.getOffHandStack();

            return mainHand.getItem() instanceof GhostScissorsItem ||
                    offHand.getItem() instanceof GhostScissorsItem;
        }
        return false;
    }

    @Inject(
            method = "getStatusEffects",
            at = @At("RETURN"),
            cancellable = true
    )
    private void smfs$modifyStatusEffects(CallbackInfoReturnable<Collection<StatusEffectInstance>> cir) {
        // 只有当持有鬼剪刀时，才显示诅咒效果（不过滤）
        // 如果不持有鬼剪刀，就过滤掉诅咒效果
        if (!smfs$hasGhostScissors()) {
            Collection<StatusEffectInstance> originalEffects = cir.getReturnValue();
            if (originalEffects != null && !originalEffects.isEmpty()) {
                Collection<StatusEffectInstance> filteredEffects = originalEffects.stream()
                        .filter(effect -> !(effect.getEffectType() instanceof ICurseEffect))
                        .collect(Collectors.toList());
                cir.setReturnValue(filteredEffects);
            }
        }
    }
}